package com.example.ymm.config;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class ConnectionPoolInfoPrinter implements CommandLineRunner {

    @Autowired
    private DataSource dataSource;

    @Override
    public void run(String... args) throws Exception {
        // 判断是否为Hikari连接池（Spring Boot默认）
        if (dataSource instanceof HikariDataSource hikariDataSource) {
            System.out.println("===== 数据库连接池信息 =====");
            System.out.println("连接池类型: HikariCP");
            System.out.println("驱动类: " + hikariDataSource.getDriverClassName());
            System.out.println("JDBC URL: " + hikariDataSource.getJdbcUrl());
            System.out.println("用户名: " + hikariDataSource.getUsername());
            System.out.println("最大连接数: " + hikariDataSource.getMaximumPoolSize());
            System.out.println("最小空闲连接数: " + hikariDataSource.getMinimumIdle());
            System.out.println("连接超时时间(ms): " + hikariDataSource.getConnectionTimeout());
            System.out.println("空闲连接超时时间(ms): " + hikariDataSource.getIdleTimeout());
            System.out.println("连接最大存活时间(ms): " + hikariDataSource.getMaxLifetime());
            System.out.println("==========================");
        } else {
            System.out.println("使用的连接池类型: " + dataSource.getClass().getName());
        }
    }
}